package com.itbaima.csmall.business.rabbit;

import com.google.gson.Gson;
import com.itbaima.csmall.business.rabbit.config.RabbitMQConfig;
import com.itbaima.csmall.commons.pojo.order.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * <p>RabbitProducer</p>
 *
 * @author Chen
 * @since 2023/3/8 19:14:14
 */
@Slf4j
@Component
public class RabbitProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    static int i = 1;
    @Scheduled(cron = "0/10 * * * * ?")
    public void sendMessage() {
        Order order = new Order();
        order.setId(i++);
        order.setCount(10 + RandomUtils.nextInt(90));
        order.setCommodityCode("PC100");
        order.setUserId("UU100");
        order.setMoney(1000 + RandomUtils.nextInt(10000));
        Gson gson = new Gson();
        String json = gson.toJson(order);
        rabbitTemplate.convertAndSend(RabbitMQConfig.BIZ_EXCHANGE, RabbitMQConfig.BIZ_ROUTE_KEY, json);
        log.info("发出消息：{}", json);
    }

}
